/*
 * Author: Patrick Rand
 * Date: Nov 12, 2013
 */
$(function () {
    var successMsgContainer = $("div[id*=successMessageContainer]");
    var errorMsgContainer = $("div[id*=errorMessageContainer]");
    var successMsg = $("span[id*=successMessage]");
    var errorMsg = $("span[id*=errorMessage]");
    var buttonEdit = $("button[id*=editSection]");
    var buttonDelete = $("button[id*=deleteSection]");
    var buttonEnroll = $("button[id*=enrollSection]");
  //var buttonSchedule = $("button[id*=scheduleSection]");
    buttonEdit.click(setSelectedSectionValues);
    buttonEnroll.click(setSelectedSectionValues);
    buttonDelete.click(deleteSection);
    
    var $sectionsGrid = $("#sectionsGrid");
    
    if (successMsg.text()) successMsgContainer.show();
    if (errorMsg.text()) errorMsgContainer.show();
    
    loadSections();
    $sectionsGrid.bind('rowselect', function (event) {
        var rowindex = event.args.rowindex;
        
        if (rowindex > -1) {
            buttonEdit.removeClass("disabled");
            buttonDelete.removeClass("disabled");
            buttonEnroll.removeClass("disabled");
          //  buttonSchedule.removeClass("disabled");
            buttonEdit.removeAttr("disabled");
            buttonDelete.removeAttr("disabled");
            buttonEnroll.removeAttr("disabled");
           // buttonSchedule.removeAttr("disabled");
        }
        else {
            buttonEdit.class("disabled");
            buttonDelete.class("disabled");
            buttonEnroll.class("disabled");
        //    buttonSchedule.class("disabled");
            buttonEdit.attr("disabled", "disabled");
            buttonDelete.attr("disabled", "disabled");
            buttonEnroll.attr("disabled","disabled"); 
         //   buttonSchedule.attr("disabled","disabled");
        }
    });
    
    function setSelectedSectionValues() {
        var rowindex = $sectionsGrid.jqxGrid('getselectedrowindex');
        var datarow = $sectionsGrid.jqxGrid('getrowdata', rowindex);
        $("input[id*=inputSectionId]").val(datarow.SectionId);
        $("input[id*=inputSectionName]").val(datarow.SectionName);
        $("input[id*=inputProfId]").val(datarow.ProfId);
        $("input[id*=inputCourseId]").val(datarow.CourseId);
        $("input[id*=inputCourseName]").val(datarow.CourseName);
      //  $("input[id*=inputScheduleId]").val(datarow.ScheduleId);
        $("input[id*=inputSemesterTypeId]").val(datarow.SemesterTypeId);
      //  $("input[id*=inputRoomNumber]").val(datarow.RoomNumber);
        $("input[id*=inputSemesterYear]").val(datarow.SemesterYear);
    }
    
    function loadSections() {
        // prepare the data
        var source =
        {
            datatype: "json",
            datafields: [
                { name: 'SectionId', type: 'int' },
                { name: 'SectionName', type: 'string' },
                { name: 'ProfId', type: 'int' },
                { name: 'ProfessorName', type: 'string' },
                { name: 'CourseId', type: 'int' },
                { name: 'CourseName', type: 'string' },
              //  { name: 'ScheduleId', type: 'int' },
                { name: 'SemesterTypeId', type: 'int'},
                { name: 'SemesterTypeName', type: 'string'},
                { name: 'SemesterYear', type: 'int'}//,
             //   { name: 'RoomNumber', type: 'string'}
            ],
            data: { action: "loadSections" },
            url: "./adminSections"
        };

        var dataAdapter = new $.jqx.dataAdapter(source,
            {
                formatData: function (data) {
                    $.extend(data, {
                        featureClass: "P",
                        style: "full",
                        maxRows: 50
                    });

                    return data;
                }
            }
        );

        var dataAdapter = new $.jqx.dataAdapter(source, {
            downloadComplete: function (data, status, xhr) { },
            loadComplete:  function (data) {
                        return $.makeArray(data);
                    },
            loadError: function (xhr, status, error) { }
        });
        // initialize jqxGrid
        $sectionsGrid.jqxGrid(
        {
            width: 700,
            source: dataAdapter,
            theme: 'base',
            pageable: true,
            autoheight: true,
            sortable: true,
            altrows: true,
            enabletooltips: true,
            editable: false,
            columns: [
              { text: 'Section Name', datafield: 'SectionName' },
              { text: 'Course Name', datafield: 'CourseName' },
//              { text: 'Course ID', datafield: 'CourseId' },
//              { text: 'Professor ID', datafield: 'ProfId' },
              { text: 'Professor Name', datafield: 'ProfessorName' },
              { text: 'Semester Type', datafield: 'SemesterTypeName'},
              { text: 'Year', datafield: 'SemesterYear'}//,
             // { text: 'Schedule ID', datafield: 'ScheduleId' },
             // { text: 'Room Number', datafield: 'RoomNumber'}
            ]
        });
    }

     function deleteSection() {
        if (confirm("Are you sure you want to delete this section? \n All enrolled students from the section will be dropped.")) {
            setSelectedSectionValues();
            $("button[id*=submitDeleteSection]").click();
        }
    }
});
